package jp.ac.waseda.cs.washi.game.entity;

import java.awt.Image;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import jp.ac.waseda.cs.washi.game.api.PumpkinInfo;
import jp.ac.waseda.cs.washi.gameaiarena.api.Point2;
import jp.ac.waseda.cs.washi.game.api.PumpkinType;
import jp.ac.waseda.cs.washi.gameaiarena.gui.Renderer;

public class Pumpkin {
	class PumpkinInfoImpl implements PumpkinInfo {
		@Override
		public PumpkinType getType() {
			return Pumpkin.this.getType();
		}

		@Override
		public Point2 getLocation() {
			return Pumpkin.this.getLocation();
		}

		@Override
		public Point2 getCenterLocation() {
			return Pumpkin.this.getCenterLocation();
		}

		@Override
		public List<PumpkinCore> getCores() {
			return Collections.unmodifiableList(Pumpkin.this.getCores());
		}
	}

	private PumpkinType _type;
	private Point2 _location;
	private ArrayList<PumpkinCore> _cores;

	private final PumpkinInfo _pumpInfo;
	
	/**
	 * 得点計算の際に利用するフィールド
	 */
	private boolean _alive;

	public Pumpkin(PumpkinType type, Point2 location, int durability) {
		_type = type;
		_location = location;
		_pumpInfo = new PumpkinInfoImpl();
		initCores(durability);
		setAlive(true);
	}

	private void initCores(int durability) {
		_cores = new ArrayList<PumpkinCore>();
		for (Point2 p : _type.getCoreLocations())
			_cores.add(new PumpkinCore(p.add(_location), durability));
	}

	public PumpkinCore getCore(Point2 coreLocation) {
		for (PumpkinCore core : _cores) {
			if (core.getLocation().equals(coreLocation))
				return core;
		}
		return null;
	}

	public ArrayList<PumpkinCore> getCores() {
		return _cores;
	}

	public void removeCore(PumpkinCore core) {
		_cores.remove(core);
	}

	public PumpkinInfo getInfo() {
		return _pumpInfo;
	}

	public PumpkinType getType() {
		return _type;
	}

	public Point2 getLocation() {
		return _location;
	}

	public Point2 getCenterLocation() {
		return _location.add(_type.getCenterLocation());
	}

	public void draw(Renderer render, Chara owner) {
		Image[] images = owner.getPumpkinImages();

		for (PumpkinCore core : this.getCores()) {
			Point2 p = core.getLocation();
			switch (_type) {
			case MiniPumpkin:
				render.drawImage(images[0], (p.x * Map.BLOCK_WIDTH)
						- (Map.BLOCK_WIDTH / 3), (p.y * Map.BLOCK_HEIGHT)
						- (Map.BLOCK_HEIGHT / 3));
				break;
			case KingPumpkin:
				render.drawImage(images[1], (p.x * Map.BLOCK_WIDTH)
						- (Map.BLOCK_WIDTH / 2), (p.y * Map.BLOCK_HEIGHT)
						- (Map.BLOCK_HEIGHT / 3));
				break;
			}
		}
	}

	public void setAlive(boolean _alive) {
		this._alive = _alive;
	}

	public boolean isAlive() {
		return _alive;
	}
}
